package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.mapper.DepartmentMapper;
import cn.wolfcode.crm.service.IDepartmentService;
import cn.wolfcode.crm.domain.Department;
import cn.wolfcode.crm.qo.QueryObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class DepartmentService implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;
    @Override
    public void saveOrUpdate(Department department) {
        if(department.getId() != null){
            departmentMapper.updateByPrimaryKey(department);
        } else {
            departmentMapper.insert(department);
        }
    }

    @Override
    public void delete(Long id) {
        if(id != null){
            departmentMapper.deleteByPrimaryKey(id);
        }
    }

    @Override
    public Department getDepartmentById(Long id) {
        return departmentMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Department> getAll() {
        return departmentMapper.selectAll();
    }

    @Override
    public PageInfo<Department> selectByQuery(QueryObject queryObject) {
//        int totalCount = departmentMapper.countByQuery(queryObject);
//        if(totalCount == 0){
//            return new PageResult(1,queryObject.getPageSize(),0, Collections.emptyList());
//        }
        //用pageInfo   调用方法
        PageHelper.startPage(queryObject.getCurrentPage(),queryObject.getPageSize());
        List<Department> departments = departmentMapper.selectByQuery(queryObject);
        return new PageInfo<>(departments);
    }
}
